package com.kcs.fxmargin.action;



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.LoginActionForm;
import com.kcs.fxmargin.bo.MainPageBO;
import com.kcs.fxmargin.service.MainPageService;
import com.kcs.fxmargin.service.UsersService;

abstract class FXAction extends Action {
	
	private Object loginDataSession = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception {
		ActionForward forward = null;
		String actionPath = null;
		String roleId = null;
		MainPageBO mainPageBo = null;
		MainPageService mainPageSvc = null;
		LoginActionForm loginForm = null;
		
        try{
//        	System.out.println("=================================== FXAction ===============================");
        	HttpSession session = request.getSession();
//        	System.out.println("=================================== session ==============================="+session.getId());
        	
        	if(session==null){
        		
        		request.setAttribute("error_message",	 "Session time out");
        		forward = mapping.findForward("login");
        		System.out.println(" Timeout");
        		return forward;
        	} else {
        		loginDataSession = request.getSession().getAttribute(Constant.LOGIN_DATA_SESSION);
        		loginForm = (LoginActionForm)loginDataSession;
        		UsersService user = new UsersService();
        		
        		if(loginForm == null){
        			request.setAttribute("error_message",	 "Session time out");
            		forward = mapping.findForward("login");
            		System.out.println(" Timeout");
            		return forward;
        		} else {
        			String sessionIdInBase 	= user.getSessionId(loginForm.getUserId());
            		String currentSessionId = request.getSession().getId();
            		
            		if(sessionIdInBase.equals(currentSessionId)) {
            			System.out.println("session is a life");
            		} else {
            			System.out.println("session is a new session , kick all another session");
            			request.setAttribute("error_message",	 "Your session has expired because there is login from another.");
                		forward = mapping.findForward("login");
                		return forward;
            		}
        		}
        		
        	}
        	
//        	else{
        	//For test
        		initHeaderMenu(request);
        		initLoginData(request);
        		
        		//Check Role (before process()) //***
        		loginForm = (LoginActionForm)loginDataSession;
            	roleId = loginForm.getRoleId();
            	actionPath = request.getServletPath();		
            	
            	mainPageBo = new MainPageBO();
            	mainPageBo.setRoleId(roleId);
            	mainPageBo.setActionPath(actionPath);
            	
            	mainPageSvc = new MainPageService();
            	mainPageBo = mainPageSvc.getActionRoleFlag(mainPageBo);
            	
            	System.out.println(">actionPath: "+actionPath+", flag : "+mainPageBo.getFlag());
            	
            	if(!mainPageBo.getFlag().equals("1")){
            		forward = mapping.findForward("mainPage"); //*** +if else
            	}else{
            		forward = process(mapping, form, request, response);
            	}
            	//****
        		
//        	}
//        	//****
        	
        	//For Use
//        	if(initHeaderMenu(request) && initLoginData(request)){
//        		forward = process(mapping, form, request, response);
//        	}else{
//        		forward = mapping.findForward("noLogin");
//        	}
              	
    		
        		
        }catch(Exception e){
        	e.printStackTrace();
        	request.setAttribute(Constant.ERR_ATTR, e+"");
        }finally{
        	
        }
        
        return forward;
    }

	public boolean initHeaderMenu(HttpServletRequest request)throws Exception{
		Object headerMenu = request.getSession().getAttribute(Constant.HEADER_MENU_SESSION);

		if(headerMenu != null){
			request.setAttribute(Constant.HEADER_MENU_ATTR, headerMenu);
			return true;
		}else{
			return false;
		}
		
	}
	
	public boolean initLoginData(HttpServletRequest request)throws Exception{
		loginDataSession = request.getSession().getAttribute(Constant.LOGIN_DATA_SESSION);
		
		if(loginDataSession != null){
			LoginActionForm loginForm = (LoginActionForm)loginDataSession;
			String loginData = getLoginDataHTML(loginForm);
            request.setAttribute(Constant.LOGIN_DATA_ATTR, loginData);
            return true;
		}else{
			return false;
		}
	}
	
	protected String getLoginDataHTML(LoginActionForm loginForm)throws Exception{
		String html = "<div id='divLoginData'><font style='color:white;font-size:10px'>User ID: "+loginForm.getUserId()+
				      " User Name: "+loginForm.getUserName()+
				      "</font>&nbsp;&nbsp;<input type='button' class='btnLogout' value='Logout' onclick='logout();'/></div>"+
				      "<input type='hidden' id='userLoginId' name='userLoginId' value='"+loginForm.getUserId()+"' />"+
				      "<input type='hidden' id='userLoginName' name='userLoginName' value='"+loginForm.getUserName()+"' />";
		
		return html;
	}
	
	abstract public ActionForward process(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception;
}
